//
//  TF_ChapterCell.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/11/30.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit
import SDWebImage
import Lottie
class TF_ChapterCell: UITableViewCell {

    var imgView:UIImageView!
    var course:UILabel!
    var page:UILabel!
    var chapter:UILabel!
    var onPlayBtn:UIButton!
    var playAnimation = TalkfunSharedTools.getJsonDataJsonname("TF_playAnimation")
   
    var rotated:Bool = false
    var line:UIView!
    @objc public var cellwidth:CGFloat = 0
    var dataManager:TalkfunCacheImage = TalkfunCacheImage()
     
    @objc public var selectedArray:[Int] = []
    @objc public var number:NSInteger = 0

    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clear
        self.imgView = UIImageView.init()
        self.contentView.addSubview(self.imgView)
        
        
        self.page = UILabel.init()
        self.page.font = UIFont.PingFang(size: 14)
        self.page.textColor = UIColor(red: 0.54, green: 0.61, blue: 0.69,alpha:1)
        self.page.text = "第1页"
        self.page.textAlignment = NSTextAlignment.left
        self.contentView.addSubview(self.page)
        
        self.course = UILabel.init()
        self.course.font = UIFont.PingFang(size: 14)
        self.course.textColor = UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1)
        self.course.text = "第1页（00:23:01）"
        self.contentView.addSubview(self.course)
        
        self.chapter = UILabel.init()
        self.chapter.isHidden = true
        self.chapter.font = UIFont.PingFang(size: 14)
        self.chapter.textColor = UIColor(red: 0.39, green: 0.41, blue: 0.43,alpha:1)
        self.chapter.textAlignment = NSTextAlignment.right
        self.chapter.text = "第一章"
        self.contentView.addSubview(self.chapter)
        
        
        self.onPlayBtn = UIButton.init()
        self.onPlayBtn.setTitle("播放中", for: .normal)
        self.onPlayBtn.setTitleColor(UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1), for: .normal)
        self.onPlayBtn.titleLabel?.font = UIFont.PingFang(size: 14)
        self.onPlayBtn.isHidden = true
        self.contentView.addSubview(self.onPlayBtn)

        self.playAnimation.isHidden = true
        self.contentView.addSubview(self.playAnimation)
       
        self.line = UIView.init()
        self.line.backgroundColor = UIColor(red: 0.86, green: 0.89, blue: 0.92, alpha: 1)
        self.contentView.addSubview(self.line)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.imgView.frame = CGRect(x: 16, y: 12, width: 98, height: 66)
        self.page.frame = CGRect(x: self.imgView.frame.maxX+8, y: 12, width: self.cellwidth-self.imgView.frame.maxX-8, height: 20)
        self.course.frame = CGRect(x: self.imgView.frame.maxX+8, y: self.frame.size.height-12-20, width: self.cellwidth-self.imgView.frame.maxX-8, height: 20)
        
        self.chapter.frame = CGRect(x: self.cellwidth-70-24, y: (self.frame.size.height-20)/2.0, width: 70, height: 20)
        self.onPlayBtn.frame = CGRect(x: self.cellwidth-45-24, y: (self.frame.size.height-20)/2.0, width: 45, height: 20)
        self.playAnimation.frame = CGRect(x: self.onPlayBtn.frame.origin.x-25, y: (self.frame.size.height-20)/2.0, width: 20, height: 20)
        self.line.frame = CGRect(x: 0, y: self.frame.size.height-1, width: self.cellwidth, height: 1)
        
    }
    
    @objc public func setmodel(model:TFPlaybackChapterPage) {
       
        self.course.text  = model.course;
    
        let starttime = self.transToHourMinSec(time: Float(model.starttime))
      
        self.page.text = String(format: "第%d页 (%@)", model.page,starttime)
  
        self.chapter.text = String(format: "第%ld章", self.number)
     
        
        if self.selectedArray[self.number] == 1 {
            self.onPlayBtn.isHidden = false
//            self.chapter.isHidden = true
            self.playAnimation.isHidden = false
            self.backgroundColor  = UIColor(red: 0.79, green: 0.83, blue: 0.87, alpha: 0.2)
        } else {
            self.onPlayBtn.isHidden = true
//            self.chapter.isHidden = false
            self.playAnimation.isHidden = true
            self.backgroundColor = UIColor.clear
        }

     
        
        
        let cachedImage = self.dataManager.getWithUrlString(model.thumbnail)
   
        if cachedImage != nil {
            self.imgView.image = cachedImage
            return
        }
      
        if model.thumbnail.hasPrefix("http") && model.course == "黑板" {
      
        
            self.page.text = "\("黑板")\(model.page)" +  " (\(self.transToHourMinSec(time: Float(model.starttime))))"
            self.imgView.image = UIImage.init(named: "HTLive.bundle/blackboard")
        } else {
            let manager = SDWebImageManager.shared
           
          
            
            let url:URL? = URL.init(string: model.thumbnail)
          
            manager.loadImage(with: url, options: SDWebImageOptions.retryFailed) { receivedSize, expectedSize, targetURL in

            } completed: {[weak self] image, data, error, cacheType, finished, imageURL in
                guard let `self` = self else {
                    return
                }

                DispatchQueue.main.async {
                    if image != nil {
                        self.imgView.image = image
                        return
                    }

                    if (error != nil) && !model.thumbnail.hasPrefix("http") && model.course == "黑板" {
                        self.page.text = "\("黑板")\(model.page)" +  " (\(self.transToHourMinSec(time: Float(model.starttime))))"

                        self.imgView.image = UIImage.init(named: "HTLive.bundle/blackboard")
                    }

                }
            }

        }
        
        
        
        if (self.rotated) {
            self.course.isHidden = true;
            self.page.isHidden = true;
        }
        else
        {
            self.course.isHidden = false;
            self.page.isHidden = false;
        }
       
       
    }
    
    func transToHourMinSec(time: Float) -> String
    {
        let allTime: Int = Int(time)
        var hours = 0
        var minutes = 0
        var seconds = 0
        var hoursText = ""
        var minutesText = ""
        var secondsText = ""
        
        hours = allTime / 3600
        hoursText = hours > 9 ? "\(hours)" : "0\(hours)"
        
        minutes = allTime % 3600 / 60
        minutesText = minutes > 9 ? "\(minutes)" : "0\(minutes)"
        
        seconds = allTime % 3600 % 60
        secondsText = seconds > 9 ? "\(seconds)" : "0\(seconds)"
        
        return "\(hoursText):\(minutesText):\(secondsText)"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
 
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
